package com.woniuxy.controller;


import com.woniuxy.annotation.MyLog;
import com.woniuxy.entity.Log;
import com.woniuxy.entity.Emp;
import com.woniuxy.service.log.LogService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Aspect
@Component
public class SuperServlet {
    @Autowired
    private LogService logService;

    @Around("@annotation(myLog)")
    public Object OperLogAspect(ProceedingJoinPoint proceedingJoinPoint, MyLog myLog) throws Throwable {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes)
                RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        Object proceed = proceedingJoinPoint.proceed();
        Emp loginEmp = (Emp) request.getSession().getAttribute("loginEmp");
        Log log = new Log(null, loginEmp.getEmpId(), new Date(), myLog.value());
        logService.addLog(log);
        return proceed;
    }
}
